package org.teamh.gui.ingame.effects;

import org.teamh.gui.engine.model.Model;
import org.teamh.logic.datamodel.Creep;

import static org.lwjgl.opengl.GL11.*;

public class UltimateEffect extends ShotEffect{

	private static final float SCALE = 1 / 32f;
	
	private Creep creep;
	private Model model;
	
	public UltimateEffect(float startX, float startY, float startZ, float endX,
			float endY, float endZ, Creep creep, Model model) {
		super(startX, startY, startZ, endX, endY, endZ);
		
		this.creep = creep;
		this.model = model;
		
		setDuration(2000);
	}

	@Override
	public void render(float timeElapsed) {
		super.render(timeElapsed);
		
		if(creep.getHealth() <= 0) {
			getEffectManager().removeEffect(this);
			return;
		}
		
		glEnable(GL_CULL_FACE);
		glCullFace(GL_FRONT);
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		
		setEndX(creep.getWorldX());
		setEndZ(creep.getWorldZ());
		
		float distance = getDistance();
		setVecX((getEndX() - getStartX()));
		setVecY((getEndY() - getStartY()));
		setVecZ((getEndZ() - getStartZ()));
		
		glColor4f(1, 1, 1, 0.5f);
		
		glPushMatrix();
		{
			float h = (float)Math.sqrt(getVecX() * getVecX() + getVecZ() * getVecZ());
			float floorAngle = 0;
			float upAngle = 0;
			
			if(getVecZ() >= 0) {
				floorAngle = (float)Math.toDegrees(Math.asin(getVecX() / h)) - 90;
			} else {
				floorAngle = (float)Math.toDegrees(-Math.asin(getVecX() / h)) + 90;
			}
			
			upAngle = (float)Math.toDegrees(Math.asin(getVecY() / distance));
			
			glTranslatef(getStartX(), getStartY(), getStartZ());
			glRotatef(floorAngle, 0, 1, 0);
			glRotatef(upAngle, 0, 0, 1);
			glScalef(distance, SCALE, SCALE);
			model.render();
		}
		glPopMatrix();
		
		glDisable(GL_CULL_FACE);
		glDisable(GL_BLEND);
		glDisable(GL_TEXTURE_2D);
	}
	
}
